/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.taglib.core;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.taglib.TagUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>Adds parameter to enclosing tag, which implements <code>com.blandware.atleap.webapp.taglib.core.ParamParent</code>
 * interface. If no such tag is found, JspException will be thrown.<br />
 * In <code>value</code> attribute instances of <code>java.util.Collection</code>, <code>java.lang.String</code> and
 * <code>java.lang.String[]</code> are supported. If instance of another class is specified, it will be cast to
 * <code>java.lang.String</code> using <code>java.lang.String.valueOf(Object)</code> method.
 * </p>
 * <p>
 * Allowed attributes are (they are both required):
 * <ul>
 * <li>
 * <b>name</b> - name of parameter
 * </li>
 * <li>
 * <b>value</b> - value of parameter to assign
 * </li>
 * </ul>
 * </p>
 * <p><a href="ParamTag.java.html"><i>View Source</i></a></p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.6 $ $Date: 2006/08/03 10:08:11 $
 * @jsp.tag name="param"
 * body-content="empty"
 * @see java.lang.String#valueOf(Object)
 */
public class ParamTag extends SimpleTagSupport {

	protected transient final Log log = LogFactory.getLog(ParamTag.class);

	/**
	 * Name of parameter
	 */
	protected String name;

	/**
	 * Value of parameter
	 */
	protected Object value;

	/**
	 * Local parameters
	 */
	protected Map parameters = new HashMap();

	/**
     * Returns name of parameter
     *
	 * @return name of parameter
	 * @jsp.attribute required="true"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="Name of parameter"
	 */
	public String getName() {
		return name;
	}

    /**
     * Sets name of parameter
     *
     * @param name name of parameter to set
     */
	public void setName(String name) {
		this.name = name;
	}

	/**
     * Returns value of parameter
     *
	 * @return value of parameter
	 * @jsp.attribute required="true"
	 * rtexprvalue="true"
	 * type="java.lang.Object"
	 * description="Value of parameter"
	 */
	public Object getValue() {
		return value;
	}

    /**
     * Sets value of parameter
     *
     * @param value value of parameter to set
     */
	public void setValue(Object value) {
		this.value = value;
	}

    /**
     * Processes the tag
     *
     * @throws JspException
     */
	public void doTag() throws JspException {

		PageContext pageContext = (PageContext) getJspContext();
		ParamParent parent = (ParamParent) findAncestorWithClass(this, ParamParent.class);
		if ( parent == null ) {
			JspTagException e = new JspTagException("This tag is only valid when nested within tag which implements 'com.blandware.atleap.webapp.taglib.core.ParamParent' interface");
			TagUtils.getInstance().saveException(pageContext, e);
		}

		// cast value to string
		if ( !(value instanceof String) && !(value instanceof String[]) && !(value instanceof Collection) ) {
			value = String.valueOf(value);
		}

		parent.addParameter(name, value);

	}

}
